//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    isLogin: true,
    canGetUserInfo:true
  },
  onLoad: function () {
    this.setData({
      isGetUserInfo: true
    })
    wx.showTabBar();
    app.userInfoReadyCallback = res => {
      console.log(app.globalData.userInfo);
      this.setData({
        isLogin: Boolean(app.globalData.userInfo)
      });
      if (this.data.isLogin) {
        wx.showTabBar();
      } else {
        wx.hideTabBar();
      }
    };
    //获取轮播图
    app.request('/Advertising/AdvertisingList', 'GET', {}, res => {
      this.setData({banner: res.list});
    });
  },
  cannotGetUserInfo: function(){
    wx.showModal({
      title: '提示',
      content: '请勾选同意获取微信基本信息',
      showCancel:false
    })
  },
  onGotUserInfo:function(e){
    if (e.detail.userInfo) {
      // 允许授权
      app.updateUserInfo({
        userId: app.globalData.userId,
        headPortrait: e.detail.userInfo.avatarUrl,
        nickName: e.detail.userInfo.nickName,
        sex: e.detail.userInfo.gender
        })
        .then(app.queryUsreInfo)
        .then(res => {
          if (app.globalData.userInfo.phoneNumber) {
            this.setData({ isLogin: true });
            wx.showTabBar();
          } else {
            this.setData({ isLogin: false });
            wx.redirectTo({
              url: '../login/login',
            });
          }
        });
    }
  },

  handleUserInfoCheck: function (e) {
    this.setData({ canGetUserInfo: !this.data.canGetUserInfo})
  }
})
